Spring Boot WebClient একটি অত্যাধুনিক HTTP ক্লায়েন্ট, যা Spring WebFlux ফ্রেমওয়ার্কের অংশ। এটি অ্যাসিনক্রোনাস এবং রিঅ্যাকটিভ প্রোগ্রামিংয়ের জন্য উপযুক্ত। নিচে উদাহরণসহ WebClient ব্যবহার সম্পর্কে বিস্তারিত আলোচনা করা হয়েছে।
Spring Boot প্রজেক্টে WebClient ব্যবহার করতে চাইলে আগে spring-boot-starter-webflux
ডিপেন্ডেন্সি যুক্ত করুন।
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfig {
@Bean
public WebClient webClient(WebClient.Builder builder) {
return builder
.baseUrl("https://api.example.com")
.build();
}
}
import org.springframework.web.reactive.function.client.WebClient;
public class WebClientGetExample {
private final WebClient webClient;
public WebClientGetExample(WebClient webClient) {
this.webClient = webClient;
}
public String fetchData() {
return webClient.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class) // Response body as String
.block(); // Blocking call (for synchronous)
}
public static void main(String[] args) {
WebClient webClient = WebClient.builder().baseUrl("https://api.example.com").build();
WebClientGetExample example = new WebClientGetExample(webClient);
String response = example.fetchData();
System.out.println("Response: " + response);
}
}
import org.springframework.web.reactive.function.client.WebClient;
public class WebClientPostExample {
private final WebClient webClient;
public WebClientPostExample(WebClient webClient) {
this.webClient = webClient;
}
public String sendData(MyRequestBody requestBody) {
return webClient.post()
.uri("/data")
.bodyValue(requestBody)
.retrieve()
.bodyToMono(String.class)
.block();
}
public static void main(String[] args) {
WebClient webClient = WebClient.builder().baseUrl("https://api.example.com").build();
WebClientPostExample example = new WebClientPostExample(webClient);
MyRequestBody requestBody = new MyRequestBody("value1", "value2");
String response = example.sendData(requestBody);
System.out.println("Response: " + response);
}
}
class MyRequestBody {
private String field1;
private String field2;
// Constructors, Getters, and Setters
public MyRequestBody(String field1, String field2) {
this.field1 = field1;
this.field2 = field2;
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
}
webClient.put()
.uri("/data/123")
.bodyValue(new MyRequestBody("updatedValue1", "updatedValue2"))
.retrieve()
.bodyToMono(Void.class)
.block();
webClient.delete()
.uri("/data/123")
.retrieve()
.bodyToMono(Void.class)
.block();
webClient.get()
.uri("/data")
.retrieve()
.onStatus(status -> status.is4xxClientError(),
clientResponse -> Mono.error(new RuntimeException("Client Error!")))
.onStatus(status -> status.is5xxServerError(),
clientResponse -> Mono.error(new RuntimeException("Server Error!")))
.bodyToMono(String.class)
.block();
WebClient webClient = WebClient.builder()
.baseUrl("https://api.example.com")
.defaultHeader("Authorization", "Bearer your-token-here")
.build();
String response = webClient.get()
.uri("/protected-resource")
.retrieve()
.bodyToMono(String.class)
.block();
System.out.println("Response: " + response);
import reactor.core.publisher.Flux;
public void fetchDataStream() {
Flux<String> dataStream = webClient.get()
.uri("/stream")
.retrieve()
.bodyToFlux(String.class);
dataStream.subscribe(data -> System.out.println("Received: " + data));
}
application.properties
webclient.default.read-timeout=5000
webclient.default.connect-timeout=3000
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebClientController {
private final WebClient webClient;
public WebClientController(WebClient webClient) {
this.webClient = webClient;
}
@GetMapping("/fetch-data")
public String fetchData() {
return webClient.get()
.uri("/data")
.retrieve()
.bodyToMono(String.class)
.block();
}
}
RestTemplate
-এর তুলনায় আরও আধুনিক এবং ফিচার-সমৃদ্ধ।Read more